<template>
  <div class="abs_wrapper">
    <div
      class="abc-item"
      v-for="item in letters"
      :key="item"
      :ref="item"
      @click="handleClick"
      @touchstart.prevent="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
    >{{ item }}</div>
  </div>
</template>

<script>
import { clearTimeout, setTimeout } from 'timers'
export default {
  nmae: 'AbcArray',
  data () {
    return {
      ifmove: false,
      startY: 0, // 提升代码性能
      timer: null // 提升代码性能
    }
  },
  props: {
    citys: Object
  },
  computed: { // 计算属性
    letters () {
      const letters = []
      for (let i in this.citys) {
        letters.push(i)
      }
      return letters
    }
  },
  updated () {
    this.startY = this.$refs['A'][0].offsetTop
  },
  methods: {
    handleClick (e) {
      this.$emit('change', e.target.innerHTML)
    },
    handleTouchStart () {
      this.ifmove = true
    },
    handleTouchMove (e) {
      if (this.ifmove) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const touchY = e.touches[0].clientY - 85
          const index = Math.floor((touchY - this.startY) / 18)
          this.$emit('change', this.letters[index])
        }, 16)
      }
    },
    handleTouchEnd () {
      this.ifmove = false
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl'
  .abs_wrapper
    position: absolute
    top: 1.7rem
    right: 0
    bottom: 0
    display: flex
    flex-direction: column
    justify-content: center
    padding: .08rem
    text-align: center
    .abc-item
      line-height: .36rem
      color: $bgColor
</style>
